package com.ilove.video.frame;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;

import com.ilove.core.ILOVE;


public class VideoCanvas extends Canvas implements PlayerListener,
		CommandListener {
	private ILOVE midlet;
    private Command stop = new Command("Stop",Command.OK,0);
    private Command exit = new Command("Exit",Command.BACK,0);
    private String url ="";
    private String status = "Press left softkey";
    private String status2 = "";
    private Player player = null;
    private VideoControl control = null;

    public VideoCanvas(ILOVE midlet) {

        this.midlet = midlet;
        System.out.println("In videoCanvas...."+url);
        addCommand(stop);
        addCommand(exit);
        setCommandListener(this);
        this.setFullScreenMode(true);
    }

    public void commandAction(Command c, Displayable arg1) {

        if(c == stop) {
            stop();
        } else if(c == exit) {
            midlet.notifyDestroyed();
        }
    }

    protected void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(0,0,0);
        g.drawString(status2,0,0,Graphics.LEFT|Graphics.TOP);
        g.drawString(status,getWidth(),getHeight(),Graphics.RIGHT|Graphics.BOTTOM);
    }

    public void start(String url) {
        System.out.println("path in start is "+url);
        try {
        	player = Manager.createPlayer(url);
            //  player = Manager.createPlayer(path);
            player.addPlayerListener(this);
            player.realize();
            control = (VideoControl)player.getControl("VideoControl");
            if (control != null) {
                control.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);
                control.setDisplayFullScreen(true);
                control.setDisplaySize(176,144);
                int width = control.getSourceWidth();
                int height = control.getSourceHeight();
                status2 = "Before: SW=" + width + "-SH=" + height + "-DW=" + control.getDisplayWidth() + "-DH=" + control.getDisplayHeight();
            }

            player.start();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        if(player != null) {
            player.deallocate();
            player.close();
        }
    }

    public void playerUpdate(Player p, String s, Object o) {
        status = s;
        if(p.getState() == Player.STARTED) {
            int width = control.getDisplayWidth();
            int height = control.getDisplayHeight();
            control.setDisplayLocation((getWidth() - width)/2,(getHeight() - height)/2);
            control.setVisible(true);
            status = s + ": DW=" + width + "-DH=" + height + "-SW=" + control.getSourceWidth() + "-SH=" + control.getSourceHeight();
        }
        repaint();
        setTitle(status);
    }

}